package com.hx.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.tags.Param;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.plaf.SpinnerUI;
import java.util.Arrays;
import java.util.Map;

@Controller
public class HelloController {

    //跳转首页
    @RequestMapping("/")
    public String index() {
        System.out.println("请求访问接口的action成功!");
        return "index";
        /*
        WEB -INF/templates/index.html
        prefx
         */
    }

    //请求的转发
    @RequestMapping("/target")
    public String target() {
        System.out.println("跳转成功");
        return "traget";
    }

    @RequestMapping(value = {"/textvalue1", "/textvalue2"})
    public String textvalue() {
        return "success";
    }

    @RequestMapping(value = "textMethod", method = {RequestMethod.GET, RequestMethod.POST})
    public String textMethod() {
        return "success";
    }

    @RequestMapping(value = {"/textParam"})
    public String textParam(String username, String password) {
        System.out.println(username + password);
        return "success";
    }

    @RequestMapping(value = {"/testParam"}, params = {"username", "password"})
    public String testParam(String username, String password) {
        System.out.println(username + password);
        return "success";
    }

    @RequestMapping(value = {"/testParam2"}, params = {"!username", "password"})
    public String testParam2(String username, String password) {
        System.out.println(username + password);
        return "success";
    }

    @RequestMapping(value = {"/testParam3"}, params = {"username=tom", "password=123"})
    public String testParam3(String username, String password) {
        System.out.println(username + password);
        return "success";
    }

    @RequestMapping(value = {"/textHeader"}, params = {"username", "password"}, headers = ("Host=localhost:8081"))
    public String textHeader() {
        return "success";
    }

    @RequestMapping(value = {"/textHeader"}, params = {"username", "password"}, headers = ("!Host=localhost:8081"))
    public String textHeader1() {
        return "success";
    }

    @RequestMapping("/textAnt")
    public String textAnt() {
        return "success";
    }

    @RequestMapping("/textPath/{id}")
    public String textPath(@PathVariable(value = "id") Integer ids) {
        System.out.println(ids);
        return "success";
    }

    @RequestMapping("/textServletAPI")
    public String textServletAPI(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username + password);
        return "success";
    }

    @RequestMapping("/textMethodParam")
    public String textMethodParam(String username, String password) {
        System.out.println(username + password);
        return "success";
    }

    @RequestMapping("/textMethodParam2")
    public String textMethodParam2(String username, String password, String[] happy) {
        System.out.println(username + password + Arrays.toString(happy));
        return "success";
    }

    @RequestMapping("/testRequestParam")
    //形参位置的request表示当前的请求的封装对象
    public String testRequestParam(@RequestParam(value = "user_name", required = true, defaultValue = "张三") String username, String password) {
        System.out.println(username + password);
        return "success";
    }

    @RequestMapping("/testRequestHeader")
    //形参位置的request表示当前的请求的封装对象
    public String testRequestHeader(String username, String password, @RequestHeader(value = "host") String host) {
        System.out.println(username + password);
        System.out.println(host);
        return "success";
    }

    @RequestMapping("/testCookieValue")
    public String testCookieValue(HttpServletRequest request, @CookieValue(required = false) String JSESSIONID) {
        HttpSession session = request.getSession();
        final String username = request.getParameter("username");
        final String password = request.getParameter("password");
        System.out.println(JSESSIONID);
        System.out.println(username + password);
        return "success";
    }

    @RequestMapping("/testPOJO")
    public <User> String testPOJO(User user) {
        System.out.println(user);
        return "success";

    }

    @RequestMapping("/testRequestByServletAPI")
    public String testRequestByServletAPI(HttpServletRequest request) {
        request.setAttribute("username", "hello servlet");
        return "success";
    }

    @RequestMapping("/textModelAndView")
    public ModelAndView textModelAndView() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("MAV", "Hello Model");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/textModel")
    public String textModel(Model model){
        model.addAttribute("Model","Hello Model");
        return "success";
    }

    @RequestMapping("/textMap")
    public String textMap(Map map){
        map.put("map","Hello Map");
        return "success";
    }
    @RequestMapping("/textModelMap")
    public String textModelMap(ModelMap modelMap){
        modelMap.addAttribute("modelMap", "Hello modelMap");
        return "success";
    }

    @RequestMapping("/testSession")
    public String testSession(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("name", "Hello Session");
        return "success";
    }
    @RequestMapping("/testApplication")
    public String testApplication(HttpServletRequest request){
        final ServletContext context = request.getSession().getServletContext();
        context.setAttribute("name", "Hello Application");
        return "success";
    }
    @RequestMapping(value = "/testThymeleafView")
    public String testThymeleafView(){
        return "success";
    }
    @RequestMapping("/testForward")
    public String testForward(){
        return "forward:/testThymeleafView";
    }
    @RequestMapping("/testRedirect")
    public String testRedirect(){
        return "redirect:http://www.baidu.com";
    }
    @RequestMapping("/text1")
    public String text1(){
        return "index";
    }

}
